package com.css.apps.base.recycle;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.css.apps.base.recycle.model.RTrash;
import com.css.db.query.TransactionCache;
import com.css.util.UuidUtil;

public class TrashCollector implements ITrash {
	
	private TransactionCache tx;

	public TransactionCache getTx() {
		return tx;
	}

	public void setTx(TransactionCache tx) {
		this.tx = tx;
	}

	@Override
	public <T> T delete(T srcObj, T obj) throws Exception {
		RTrash rTrash = (RTrash) obj;
		String entityId = UuidUtil.getUuid();
		rTrash.setEntityId(entityId);
		rTrash.setCreatedTime(new Date());
		Class<?> clazz = srcObj.getClass();
		rTrash.setEntityType(clazz.toString());
		tx.save(rTrash);
//		RTrashContent rTrashContent = new RTrashContent();
		/*rTrashContent.setEntityId(entityId);
		rTrashContent.setEntityContent(serializeObject(srcObj));
		tx.save(rTrashContent);*/
		return obj;
	}
	
	/*public <T> T deserializeObject(Class<T> clazz, Serializable id) {
		try {
			String id = "2adc41c8e3f64716b8174d81577b0261";
			RTrashContent rTrashContent = QueryCache.get(RTrashContent.class, id);
			ByteArrayInputStream bais = new ByteArrayInputStream(rTrashContent.getEntityContent());
			
			ObjectInputStream ois = new ObjectInputStream(bais);
			FwForm fwForm = (FwForm) ois.readObject();
			ois.close();
			bais.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	}*/
	
	public <T> byte[] serializeObject(T obj) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			oos.writeObject(obj);
			oos.flush();
			oos.close();
			baos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return baos.toByteArray();
	}
	
	/**
	 * 单条实体记录删除
	 * @param <T>
	 * @param clazz
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public <T> T delete(Class<T> clazz, Serializable id) throws Exception {
		
		return null;
	}
	
	/**
	 * 批量删除
	 * @param <T>
	 * @param clazz
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	public <T> List<T> delete(Class<T> clazz, Serializable[] ids) throws Exception {
		return null;
	}
	
	public static void main(String[] args) {
		//示例代码
//		TransactionCache tx = new TransactionCache();
//		try {
//			FwForm fwForm = new FwForm();
//			//删除自己表中实体对象，以FwForm举例
//			tx.delete(fwForm);
//			//保存实体对象到回收站表
//			TrashCollector tc = new TrashCollector();
//			tc.setTx(tx);
//			RTrash rTrash = new RTrash();
//			//设置属性
//			String funcId = "";
//			
//			rTrash.setFuncId(funcId);
//			tc.delete(fwForm, rTrash);
//			
//			RTrashContent rTrashContent = new RTrashContent();
//			
//			
//			rTrashContent.setEntityContent(null);
//			tx.commit();
//		} catch (Exception e) {
//			if (tx != null) {
//				tx.rollback();
//			}
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally {
//			if (tx != null) {
//				tx = null;
//			}
//		}
	}

}
